package com.haotunet.app.goldtreasure.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.model.BaseModel;
import com.haotunet.app.goldtreasure.model.CardInfo;
import com.haotunet.app.goldtreasure.model.CardModel;
import com.haotunet.app.goldtreasure.net.ActionUtil;
import com.haotunet.app.goldtreasure.other.Interface.OnPassBackListener;
import com.haotunet.app.goldtreasure.utils.UIHelper;
import com.haotunet.app.goldtreasure.views.TitleView;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class MyBankCardActivity extends BaseActivity {

    private final int GET_MY_BANKCARD_LIST_SUCCESS = 1;
    @InjectView(R.id.title_view)
    TitleView titleView;
    @InjectView(R.id.no_card_layout)
    LinearLayout noCardLayout;
    @InjectView(R.id.bank_icon)
    ImageView bankIcon;
    @InjectView(R.id.bank_name)
    TextView bankName;
    @InjectView(R.id.card_tail_num)
    TextView cardTailNum;
    @InjectView(R.id.btn_delete)
    Button btnDelete;
    @InjectView(R.id.had_card_layout)
    LinearLayout hadCardLayout;
    private boolean isDelete = true;
    private CardInfo cardInfo;
    private Handler handle = new Handler() {

        public void handleMessage(Message msg) {
            switch (msg.what) {
                case GET_MY_BANKCARD_LIST_SUCCESS:
                    CardModel model = (CardModel) msg.obj;
                    List<CardInfo> cardList = model.getBankCards();
                    if (cardList.size() == 0) {
                        noCardLayout.setVisibility(View.VISIBLE);
                        return;
                    }
                    titleView.getRightText().setVisibility(View.VISIBLE);
                    hadCardLayout.setVisibility(View.VISIBLE);
                    cardInfo = cardList.get(0);
                    bankName.setText(cardInfo.getBankName());
                    cardTailNum.setText("尾号" + cardInfo.getCardNo().substring(cardInfo.getCardNo().length() - 4,
                            cardInfo.getCardNo().length()));
                    Glide.with(MyBankCardActivity.this).load(cardInfo.getBankCardLogo()).into(bankIcon);
                    break;

                default:
                    break;
            }
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_bankcard);
        ButterKnife.inject(this);
        initView();
        initData();
    }

    private void initData() {
        ActionUtil actionUtil = new ActionUtil(MyBankCardActivity.this);
        actionUtil.getMyBankCardList();
        actionUtil.setOnPassBackListener(new OnPassBackListener() {

            @Override
            public void passBack(BaseModel model) {
                if (isFinishing()) {
                    return;
                }
                CardModel cardModel = (CardModel) model;
                if (cardModel == null) {
                    UIHelper.showToast(MyBankCardActivity.this, getString(R.string.rank_fail));
                    return;
                }
                if (cardModel.getErrorMsg() != null) {
                    UIHelper.showToast(MyBankCardActivity.this, cardModel.getErrorMsg());
                } else {
                    Message msg = new Message();
                    msg.what = GET_MY_BANKCARD_LIST_SUCCESS;
                    msg.obj = cardModel;
                    handle.sendMessage(msg);
                }
            }
        });
    }

    private void initView() {
        titleView.getTitletextView().setVisibility(View.VISIBLE);
        titleView.getTitletextView().setText(getString(R.string.title_bank_card));
        titleView.getRightText().setText(getString(R.string.manage));
        titleView.getBackView().setVisibility(View.VISIBLE);
    }

    protected void deleteBindCard(String card_no) {

        ActionUtil actionUtil = new ActionUtil(MyBankCardActivity.this);
        actionUtil.deleteBindCard(card_no);
        actionUtil.setOnPassBackListener(new OnPassBackListener() {

            @Override
            public void passBack(BaseModel model) {
                if (isFinishing()) {
                    return;
                }
                if (model == null) {
                    UIHelper.showToast(MyBankCardActivity.this, MyBankCardActivity.this.getString(R.string.rank_fail));
                    return;
                }
                if (model.getErrorMsg() != null) {
                    UIHelper.showToast(MyBankCardActivity.this, model.getErrorMsg());
                } else {
                    UIHelper.showToast(MyBankCardActivity.this, "删卡成功");
                    hadCardLayout.setVisibility(View.GONE);
                }
            }
        });
    }

    @OnClick({R.id.txt_right, R.id.iv_back, R.id.btn_delete})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.txt_right:
                if (isDelete) {
                    btnDelete.setVisibility(View.VISIBLE);
                    titleView.getRightText().setText(getString(R.string.action_negative));
                    isDelete = false;
                } else {
                    btnDelete.setVisibility(View.GONE);
                    titleView.getRightText().setText(getString(R.string.manage));
                    isDelete = true;
                }
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_delete:
                if (cardInfo != null) {
                    deleteBindCard(cardInfo.getCardNo());
                }
                break;
        }
    }
}
